package cn.enilu.guns.service.door;


import cn.enilu.guns.bean.dto.EventRecordDto;
import cn.enilu.guns.bean.entity.door.EventRecord;
import cn.enilu.guns.dao.door.EventRecordRepository;

import cn.enilu.guns.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import cn.enilu.guns.bean.vo.query.Page;

@Service
public class EventRecordService extends BaseService<EventRecord,Long,EventRecordRepository>  {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private EventRecordRepository eventRecordRepository;


    public Page<EventRecordDto> queryEventRecordDtoPage(Page<EventRecordDto> page,Long userId,String equipmentName,String beginTime,String endTime,String eventStatus) {
        Pageable pageable = null;
        if(page.isOpenSort()) {
            pageable = new PageRequest(page.getCurrent()-1, page.getSize(), page.isAsc() ? Sort.Direction.ASC : Sort.Direction.DESC, page.getOrderByField());
        }else{
            pageable = new PageRequest(page.getCurrent()-1,page.getSize(), Sort.Direction.DESC,"time_second");
        }
        org.springframework.data.domain.Page<EventRecordDto> pageResult  = eventRecordRepository.queryEventRecordDto(userId,equipmentName,beginTime,endTime,eventStatus,pageable);
        page.setTotal(Integer.valueOf(pageResult.getTotalElements()+""));
        page.setRecords(pageResult.getContent());
        return page;
    }
}

